package cn.jingzhuan.fm.ui.fragment

import android.content.Context
import android.os.Bundle
import android.view.WindowManager
import android.widget.Toast
import cn.jingzhuan.fm.R
import cn.jingzhuan.fm.databinding.FragmentFmAudioCommentSendBinding
import cn.jingzhuan.fm.mvp.contract.FMAudioCommentSendContract
import cn.jingzhuan.fm.mvp.presenter.FMAudioCommentSendPresenter
import cn.jingzhuan.fm.ui.activity.FMAudioCommentActivity
import javax.inject.Inject
import android.view.inputmethod.InputMethodManager.HIDE_NOT_ALWAYS
import android.content.Context.INPUT_METHOD_SERVICE
import android.view.inputmethod.InputMethodManager


class FMAudioCommentSendFragment : BaseBindingBottomSheetDialogFragment<FragmentFmAudioCommentSendBinding>() ,FMAudioCommentSendContract.View{


    @Inject
    lateinit var presenter: FMAudioCommentSendPresenter
    lateinit var audioId: String

    companion object {
        val ARG_AUDIO_ID = "audioId"
        fun newInstance(audioId: String): FMAudioCommentSendFragment {
            val args = Bundle()
            val fragment = FMAudioCommentSendFragment()
            args.putString(ARG_AUDIO_ID, audioId)
            fragment.arguments = args
            return fragment
        }
    }

    override fun onDataBind(savedInstanceState: Bundle?, binding: FragmentFmAudioCommentSendBinding) {
        audioId = arguments!!.getString(ARG_AUDIO_ID)
//        binding.commentContent.requestFocus();
//        activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//        val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
//        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
        initListener()
    }

    private fun initListener() {
        this.mBinding.commentCancel.setOnClickListener {
            // 关闭弹窗
            this.dismiss()
        }

        this.mBinding.commentSend.setOnClickListener {
            presenter.sendComment(audioId, this.mBinding.commentContent.text.toString())
        }
    }

    override fun getLayoutId(): Int = R.layout.fragment_fm_audio_comment_send

    override fun onSendCommentOver(result: Boolean, msg: String) {
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show()
        if (result) {
            var commentActivity = activity as FMAudioCommentActivity
            commentActivity.presenter.refresh(audioId)
            this.dismiss()
        }
    }
}